import fn from "@/utils/common";
import config from "../config";
import routePageKey from "../utils/router/pagesKey";
uni.fn = fn;
uni.lan = {};

const NAVIGATE_HOME = -1; // 首页
const NAVIGATE_BACK = -2; // 返回
const NAVIGATE_LOGIN = -3; // 登录
const NAVIGATE_PAGE = 6; // 页面导航
uni.lan.clog = () => {
  console.log("log");
};



// 获取图片
uni.lan.getImage = (url) =>{
  return config.cosBaseUrl + url + '.png';
}

/**
 * 
 * @description 路径转换
 * @returns {string}
 */
const routFn = (pageKey,obj) =>{
  let params = ''

  if (obj) {
    params =
      '?' +
      Object.entries(obj)
        .filter(([key, value]) => value !== null)
        .map(([key, value]) => `${key}=${value}`)
        .join('&')
  }

  return routePageKey[pageKey] + params


}


uni.lan.navigate = ( url, params) => {
  const userInfo = uni.getStorageSync("user"); //获取user
 
  // 未登录处理
  // if (!userInfo.userMessage) {
  //   uni.showModal({
  //     title: "您还未登录",
  //     content: "请先登录再进行操作~",
  //     cancelText: "暂不登录",
  //     confirmText: "去登录",
  //     confirmColor: "#155dff",
  //     success: function (res) {
  //       if (res.confirm) {
  //         uni.lan.toUrl(-3);
  //       } else if (res.cancel) {
  //         // uni.lan.toUrl(-2)
  //       }
  //     },
  //   });

  //   return;
  // }
  
  if(!userInfo.userMessage){
    console.log('未登录');
  }
  console.log(`跳转到+ ${routFn(url,params)}`);

//   登录后进行跳转
  uni.navigateTo({
    url: `${routFn(url,params)}`,
    success:()=>{
      console.log(`跳转到+ ${routFn(url,params)}`);
    }
  })

};
